package Multi;


import java.awt.*;
import java.awt.event.*;
import java.io.*; 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.color.*;
import java.awt.GridLayout;

import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import java.awt.*;
class student  {
    int oldOrder;
	String college,classes,name;
	long code;
	public student() {}
	public student(int oldOrder,String cg,String cs,long code,String name){
		this.oldOrder=oldOrder;college=cg;classes=cs;this.code=code;this.name=name;
	}
	public int getOldOrder()
	{
		return oldOrder;
	}
	public String getCollege(){
		return college;
	}
	public String getClasses(){
		return classes;
	}
	public String getName(){
		return name;
	}
	public long getCode(){
		return code;
	}
	public String getString()
	{   String str=new String();
	   str=this.getOldOrder()+"	 "+this.getCollege()+"	 "+this.getClasses()+"	 "+this.getCode()+"	 "+this.getName();
		return str; 
	}
}
public class Multi extends JFrame implements ActionListener,ListSelectionListener {
   JTextField txtNumber,college,number,classes,names;    //设置单行文本框
   JButton btnInc,btnDec;   //设置两个按钮
   JList listNames=new JList();
   List cc=new ArrayList();
   int count;
   final Object[] columnNames={"考试科目","成绩"};
   Object[][] rowData={{"",""}};
   
   Multi(String sTitle){
	   super(sTitle);
	   count=this.io();
	   initComponents();
   }
 
   public void initComponents(){        //学院，班级，学号，姓名
	   Container c=getContentPane();
	   //c.setLayout(new FlowLayout(FlowLayout.CENTER));   //采用流式布局，一行一行地设置
	   c.setLayout(new FlowLayout());
	   JTable table2=new JTable(rowData,columnNames);	
		table2.setRowHeight(30);
		table2.setRowHeight(0,20);
		table2.setRowMargin(5);
		table2.setRowSelectionAllowed(true);
	//	table2.setSelectionBackground(Color.BLUE);
	//	table2.setSelectionForeground(Color.WHITE);
		table2.setGridColor(Color.BLACK);
		table2.setShowHorizontalLines(true);
		table2.setShowHorizontalLines(false);
//		table2.setValueAt("",0,0);  动态初始设置
		table2.doLayout();
	//	table2.setBackground(Color.lightGray);
		table2.setPreferredScrollableViewportSize(new Dimension(600,100));
		JScrollPane pane2=new JScrollPane(table2);
		JPanel pan=new JPanel(new GridLayout(0,1));
		pan.setPreferredSize(new Dimension(200,60));
		pan.setBackground(Color.BLACK);
		pan.add(pane2);
		
	   JScrollPane scrollPane=new JScrollPane(listNames);
	   listNames.addListSelectionListener(this);
	   c.add(scrollPane);
	   InitFonts();
	   college=new JTextField("0",20);
	   classes=new JTextField("0",20);
	   number=new JTextField("0",20);
	   names=new JTextField("0",20);
	   txtNumber=new JTextField("0",20);   //初始化文本框的内容
	   
	   c.add(new JLabel("学号"));   //在文本框前添加一个标签
	   c.add(txtNumber);            //把文本框添加到界面容器中去
	   c.add(new JLabel("学院"));
	   c.add(college);
	   c.add(new JLabel("班级")); 
	   c.add(classes); 
	   c.add(new JLabel("姓名"));
	   c.add(names); 
	   btnInc=new JButton("保存");
	   c.add(pan);
	   c.add(btnInc);
	   btnInc.addActionListener(this);   //注册监听

	   txtNumber.setEditable(false);    //设置文本框为只读
	   college.setEditable(false);classes.setEditable(false);
	   names.setEditable(false);

	   try{
		   String lfClassName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		   UIManager.setLookAndFeel(lfClassName);
		   SwingUtilities.updateComponentTreeUI(this);
		   
	   }
	   catch (Exception e ){}
	   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   pack();
   }
   private void InitFonts(){  //列表数据初始化,读取txt文件内容到数组中去
	  
	   String[] names2=new String[count];
	   for(int i=0;i<count;i++)
	   {   student temp=new student();
	       temp=(student)cc.get(i);
		   names2[i]=Long.toString(temp.getCode());
	   }
	   
	   listNames.setListData(names2);
	   
	   
   }
   public void valueChanged(ListSelectionEvent e)  {

	   
	  txtNumber.setText(String.valueOf(listNames.getSelectedValue()));//获取当前选中的内容，放到文本框中显示
	  String now=String.valueOf(listNames.getSelectedValue());
	   student temp=new student();
	   student temp2=new student();
	   String temp3;
	   for(int i=0;i<count;i++)
	   {   
	       temp=(student)cc.get(i);
	       temp3=Long.toString(temp.getCode());
           if(now.equals(temp3))
             temp2=temp;
    	 
	   }
	   college.setText(String.valueOf(temp2.getCollege()));
	   classes.setText(temp2.getClasses());
	   names.setText(temp2.getName());
   }
   public void actionPerformed(ActionEvent e){   //设置相应的内容
	 int oldNum=Integer.parseInt(txtNumber.getText());//获取当前文本框内的内容
	   int newNum=oldNum;
	   if(e.getSource()==btnInc)
		   newNum++;
	   txtNumber.setText(String.valueOf(newNum));
	  
	   
	   
   }
   public static ArrayList find(String str){
   	int c=0;
   	ArrayList a=new ArrayList();
   	while(c!=(-1)){
   	  c=str.indexOf("\t", c+1);//使用indexOf来查找制表符所在的位置
   	  a.add(c);               //动态地保存到ArrayList中去
   	}
   	return a;
   }
   public int io() {//cc保存了student的实例
	   int count=0;
	   try{
		   FileInputStream fr=new FileInputStream("Java课名单.txt");
		   InputStreamReader isr=new InputStreamReader(fr,"UTF-8");
		   BufferedReader br=new BufferedReader(isr);
	       ArrayList a=new ArrayList();//用于保存每一行字符串中的制表符的位置
		   String strLine,strtemp;		  
		   String[] temp=new String[4];
		   while(br.ready()){
			  strLine=(String)br.readLine();
			 a=Multi.find(strLine);
			 count++;                      //count中记录的是总共的行数
		     for(int i=0;i<4;i++){
		    	 int firstOne=(int)a.get(i),secondOne=(int)a.get(i+1);//firstOne:当前的制表符的位置,secondOne:下一个制表符的位置
		      	 strtemp=strLine.substring(firstOne+2,secondOne);//获取两个制表符中间的字符串
		      	 temp[i]=strtemp;
		     }  
		     student newStudent=new student(count,temp[0],temp[1],Long.parseLong(temp[2]),temp[3]);
		     //创建student对象
		     cc.add(newStudent);//保存student对象到数组中去
		    }		 
		   br.close();
		   fr.close();		 
		   isr.close();

		   
		   
	   }
	   catch (IOException e) {
		   e.printStackTrace();
	   }
	   return count;
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
      Multi f=new Multi("监听多个组件事件");
      f.setVisible(true);
	}

}
